package com.comp437.uno.view.playroom;



import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.ServiceConnection;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.comp437.uno.model.ClientService;
import com.comp437.uno.model.Model;
import com.comp437.uno.model.ClientService.LocalBinder;
import com.comp437.uno.view.playboard.PlayBoard;
import com.example.comp437uno.R;

public class PlayRoomView extends Activity{

	private ListView listView;
	private Model model;
	private Intent intent;
	private TextView roomID;
	private Button test;
	 public static Handler handler; 
		Intent service;
		
		ClientService cs;
		boolean myBound=false;
	public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        model=Model.getModel();
	       
	        setContentView(R.layout.playroom);
	        intent = getIntent();
	        
	        roomID = (TextView) findViewById(R.id.textview_playroom_roomid);
	        roomID.setText(intent.getStringExtra("roomID"));

	        
	
	       
	    	service = new Intent(PlayRoomView.this, ClientService.class);
	    	bindService(service, mConnection, Context.BIND_AUTO_CREATE);
	    	
	    	 handler = new Handler(){ 
	    		 
		            @Override 
		            public void handleMessage(Message msg) { 
		                super.handleMessage(msg); 
		                	startGame();
		               
		             
		            } 
		             
		        }; 
		        model.setPlayRoomView(this);
	    	
	}
	
	@Override
	public void onBackPressed()
	{
		   new AlertDialog.Builder(this)
	        .setTitle("Really Exit?")
	        .setMessage("Are you sure you want to exit Room?")
	        .setNegativeButton(android.R.string.no, null)
	        .setPositiveButton(android.R.string.yes, new OnClickListener() {

	            public void onClick(DialogInterface arg0, int arg1) {
	                PlayRoomView.super.onBackPressed();
	                model.askForRoomList();
	            }
	        }).create().show();
	}
	
	public void startGame(){
		Intent intent=new Intent();
		intent.setClass(PlayRoomView.this, PlayBoard.class);	
		PlayRoomView.this.startActivity(intent);
	
	}
	
	class testOnClickListener implements android.view.View.OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent intent=new Intent();
			intent.setClass(PlayRoomView.this, PlayBoard.class);	
			PlayRoomView.this.startActivity(intent);
			
		}
		
	}
	 
	private ServiceConnection mConnection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName className,
                IBinder service) {
            // We've bound to LocalService, cast the IBinder and get LocalService instance
            LocalBinder binder = (LocalBinder) service;
            cs = binder.getService();
            myBound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName arg0) {
            myBound = false;
        }
    };

	
}
